﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kira.Compiler.Bods
{
    [XmlElementMapping("DITransformCall", "name", "Map_Operation")]
    public class DIMapOperation : BodsTransform
    {
        public String InputName
        {
            get
            {
                return this.GetElement("DIInputView").GetAttrbuteValue("name");
            }
        }

        public String OutputName
        {
            get
            {
                return this.GetElement("DISchema").GetAttrbuteValue("name");
            }
        }

        public MapOperationType OnDelete
        {
            get
            {
                return (MapOperationType)Enum.Parse(typeof(MapOperationType), GetOperationName("delete"), true);
            }
        }

        public MapOperationType OnInsert
        {
            get
            {
                return (MapOperationType)Enum.Parse(typeof(MapOperationType), GetOperationName("insert"), true);
            }
        }

        public MapOperationType OnNormal
        {
            get
            {
                return (MapOperationType)Enum.Parse(typeof(MapOperationType), GetOperationName("normal"), true);
            }
        }
        public MapOperationType OnUpdate
        {
            get
            {
                return (MapOperationType)Enum.Parse(typeof(MapOperationType), GetOperationName("update"), true);
            }
        }

        private String GetOperationName(String actionName)
        {
            return this.GetElement("DIAttributes").GetElement(c => c.GetAttrbuteValue("name").ToUpper() == actionName.ToUpper()).GetAttrbuteValue("value");
        }

        public override IEnumerable<string> InputNames
        {
            get { return new String[] { this.InputName }; }
        }

        public override IEnumerable<string> OutputNames
        {
            get { return new String[] { this.OutputName }; }
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(GetComments("Map_Operation"));
            sb.Append("WITH ").Append(OutputName).AppendLine(" AS (");
            //on delete
            if (OnDelete != MapOperationType.Discard && OnDelete != MapOperationType.Delete)
            {
                sb.AppendLine("--on delete");
                sb.Append("SELECT * FROM ").Append(InputName).AppendLine(" WHERE _comparision_result = 'delete'").AppendLine("UNION ALL");
            }
            //on insert
            if (OnInsert != MapOperationType.Discard && OnInsert != MapOperationType.Delete)
            {
                sb.AppendLine("--on insert");
                sb.Append("SELECT * FROM ").Append(InputName).AppendLine(" WHERE _comparision_result = 'insert'").AppendLine("UNION ALL");
            }
            //on normal
            if (OnNormal != MapOperationType.Discard && OnNormal != MapOperationType.Delete)
            {
                sb.AppendLine("--on normal");
                sb.Append("SELECT * FROM ").Append(InputName).AppendLine(" WHERE _comparision_result = 'normal'").AppendLine("UNION ALL");
            }
            //on update
            if (OnUpdate != MapOperationType.Discard && OnUpdate != MapOperationType.Delete)
            {
                sb.AppendLine("--on update");
                sb.Append("SELECT * FROM ").Append(InputName).AppendLine(" WHERE _comparision_result = 'update'");
            }
            sb.AppendLine(")");
            return sb.ToString();
        }
    }

    public enum MapOperationType
    {
        Normal, Update, Insert, Delete, Discard
    }
}
